<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use think\AjaxPage;
use think\Page;
use think\Db;
class Destine extends MobileBase {

    public function index(){
        //获取8条主题数据
        $themeList = M('destine_theme')->limit(8)->select();
        //获取4条个性案例
        $destineCaseList = D("Destine")->getDestineCase(4);
        
        $this->assign('themeList',$themeList);
        $this->assign('destineCaseList',$destineCaseList);
        return $this->fetch();
        
    }


    /**
     * 分类列表显示
     */
    public function categoryList(){
    	$start = empty(I('start'))?0:I('start');
    	$limit = empty(I('limit'))?10:I('limit');
    	if(IS_POST){
    		//组装搜索条件
    		$start = empty(I('post.start'))?0:(int)I('post.start');
    		$limit = empty(I('post.limit'))?10:(int)I('post.limit');//默认查询10条
    		if(I('post.aroma_id')){
    			$where['b.aroma_id'] = (int)I('post.aroma_id');
    		}
    		if(I('post.degree')){
    			$where['b.degree'] = (int)I('post.degree');
    		}
    		if(I('post.years')){
    			$where['b.years'] = (int)I('post.years');
    		}
    		if(I('post.theme_id')){
    			$where['b.theme_id'] = (int)I('post.theme_id');
    		}
    		if(I('post.origin_id')){
    			$where['f.origin_id'] = (int)I('post.origin_id');
    		}
    		if(I('post.quantity')){
    			$where['b.quantity'] = array('LIKE','%'.(int)I('post.quantity').'%');
    		}
    	}
    	$where['a.type'] = 2;
    	$searchgoods = M('goods')->alias('a')
    	            ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name,g.title as originname')
			    	->join('destine_goods_attr b','a.goods_id=b.goods_id',"left")
			    	->join('destine_aroma c','b.aroma_id=c.id',"left")
			    	->join('destine_theme e','b.theme_id=e.id',"left")
			    	->join('suppliers f','f.suppliers_id=b.winery_id',"left")
			    	->join('origin g','g.id=f.origin_id',"left")->where($where)->where('is_on_sale=1')->order('a.shop_price')->limit($start,$limit)->select();
		
// 		echo M('goods')->getLastSql();	    	
// 		exit;
		$count = count($searchgoods);
		$num = ceil($count/$limit);	   	   	
    	$aroma = M('destine_aroma')->field('id,title')->select();//香型分类
    	$theme = M('destine_theme')->field('id,title')->select();//主题分类
    	$origin = M('origin')->where('type',0)->field('id,title')->select();//原产地分类
    	$degree = M('destine_goods_attr')->field('degree')->group('degree')->select();//酒精度分类
    	$years = M('destine_goods_attr')->field('years')->group('years')->select();//年限分类
    	$des_quantity = M('destine_quantity')->field('id,val')->select();//规格分类
    	
    	$this->assign('limit',$limit);
    	$this->assign('pagenum',$num);
    	$this->assign('aroma',$aroma);
    	$this->assign('theme',$theme);
    	$this->assign('origin',$origin);
    	$this->assign('degree',$degree);
    	$this->assign('years',$years);
    	$this->assign('des_quantity',$des_quantity);
    	$this->assign('searchgoods',$searchgoods);
    	
    	if(IS_POST){
    		if(!empty($searchgoods)){
	    		$return_arr = array(
	                    'status' => 1,
	                    'msg' => '请求成功',
	                    'data' => array('searchgoods'=>$searchgoods,'pagenum'=>$num)
	                );
    		    return $this->ajaxReturn($return_arr);
    		}else{
	    		$return_arr = array(
	                    'status' => -1,
	                    'msg' => '没有数据',
	                    'data' => null
	                );
	    		return $this->ajaxReturn($return_arr);
	    	}	
    	}
        return $this->fetch();
    }
    
    
    /**
     * 分类展示页面
     */
    public function category(){
    	$start = empty(I('start'))?0:I('start');
    	$limit = empty(I('limit'))?10:I('limit');
    	$cateType = array(
    	          '1' => array('form'=>'destine_aroma','name'=>'白酒香型','type'=>1),
    	          '2' => array('form'=>'destine_goods_attr','name'=>'酒精度','type'=>2),
    	          '3' => array('form'=>'destine_theme','name'=>'窖藏主题','type'=>3),
    	          '4' => array('form'=>'origin','name'=>'源产地','type'=>4),
    	          '5' => array('form'=>'destine_goods_attr','name'=>'年份','type'=>5),
     	);
		$type = empty(I('type'))?1:(int)I('type');
		$id = empty(I('id'))?0:(int)I('id');
		if($type == 1 && !empty($id)){
			$where = "b.aroma_id = $id";
		}
		if($type == 2 && !empty($id)){
			$where = "b.degree = $id";
		}
		if($type == 3 && !empty($id)){
			$where = "b.theme_id = $id";
		}
		if($type == 4 && !empty($id)){
			$where = "f.origin_id = $id";
		}
		if($type == 5 && !empty($id)){
			$where = "b.years = $id";
		}
		
		if($type == 5){
			$categoryList =M('destine_goods_attr')->field('years as id,years as title')->group('title')->select();
			$categoryName = array('id'=>$id,'title'=>$id);
		}
		elseif($type == 2){
			$categoryList =M('destine_goods_attr')->field('degree as id,degree as title')->group('title')->select();
			$categoryName = array('id'=>$id,'title'=>$id);
		}
		else{
		    $categoryList = M($cateType[$type]['form'])->select();	
		    $categoryName = M($cateType[$type]['form'])->where(array('id'=>$id))->find();
		}
		$goodsList = M('goods')->alias('a')
    	            ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name')
			    	->join('destine_goods_attr b','a.goods_id=b.goods_id')
			    	->join('destine_aroma c','b.aroma_id=c.id')
			    	->join('destine_theme e','b.theme_id=e.id')
			    	->join('suppliers f','f.suppliers_id=b.winery_id')
			    	->join('origin g','g.id=f.origin_id')->where($where)->where('is_on_sale=1')->order('a.shop_price')->limit($start,$limit)->select();
		//echo M('goods')->getLastSql();   	
		$this->assign('goodslist',$goodsList); 	
    	$this->assign('categorylist',$categoryList);
    	$this->assign('categoryname',$categoryName);
    	$this->assign('theCate',$cateType[$type]);

        return $this->fetch();
    }
    
    
    
    
    
    /**
     * 窖藏主题列表显示
     */
    public function themeList(){
        //获取10条主题数据
        $themeList = M('destine_theme')->limit(10)->select();
        $this->assign('themeList',$themeList);
        return $this->fetch();
    }
     
    
    /**
     * 窖藏主题详情页面
     */
    public function theme(){
        //获取主题id
        $id = I('get.id',0); // 当前分类id
        $where['a.type'] = 2;
        $where['e.id'] = $id;
        $where['a.is_on_sale'] = 1;
        $start = empty(I('start'))?0:I('start');
    	$limit = empty(I('limit'))?10:I('limit');
        $goodsList = M('goods')->alias('a')
        ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name')
        ->join('destine_goods_attr b','a.goods_id=b.goods_id')
        ->join('destine_aroma c','b.aroma_id=c.id')
        ->join('destine_theme e','b.theme_id=e.id')
        ->join('suppliers f','f.suppliers_id=b.winery_id')
        ->join('origin g','g.id=f.origin_id')->where($where)->limit($start,$limit)->order('a.shop_price')->select();
        $theme = M('destine_theme')->where("id",$id)->find();
        $this->assign("theme",$theme['title']);
        $this->assign("themeGoodsList",$goodsList);
        return $this->fetch();
    }
    
    
    /**
     * 窖藏方式列表显示
    */
    public function cellarType(){
        return $this->fetch();
    }
    
    /**
     * 白酒文化
    */
    public function cultrue(){
        return $this->fetch();
    }
    
    
    /**
     * 最新活动
    */
    public function activity(){
        return $this->fetch();
    }

    /**
     * 原产地酒厂
    */
    public function factory(){
        return $this->fetch();
    }
    
    
    
    /**
     * 体验报名
    */
    public function tasteForm(){
        return $this->fetch();
    }

    /**
     * 商品列表页
     */
    public function goodsList(){
        $id = I('get.id',0); // 当前分类id
        $lists = getCatGrandson($id);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    
    
    /**
     * 商品详情页
     */
    public function goodsInfo(){
    	
        C('TOKEN_ON',true);        
        $goodsLogic = new \app\home\logic\GoodsLogic();
        $goods_id = I("get.id");
        $goods = M('goods')->alias('a')
                         ->join('destine_goods_attr b','b.goods_id = a.goods_id')
                         ->join('destine_theme c','b.theme_id = c.id')
                         ->join('destine_aroma e','b.aroma_id = e.id')
                         ->join('suppliers f','b.winery_id = f.suppliers_id')
                         ->join('origin g','g.id = f.origin_id')
                         ->field('a.*,b.*,g.title as originname,f.suppliers_address,
                         f.suppliers_name,f.brand_name,f.suppliers_logo,f.suppliers_id,
                         c.title as themename,e.title as aromaname')
                         ->where('b.goods_id',$goods_id)->find();              
        if(empty($goods)){
      	    $this->error('此商品不存在或者已下架');
        }                 

        M('Goods')->where("goods_id=$goods_id")->save(array('click_count'=>$goods['click_count']+1 )); //统计点击数

    	$goods['sale_num'] = M('order_goods')->where(['goods_id'=>$goods_id,'is_send'=>1])->count();
    	$goods_images_list = M('GoodsImages')->where("goods_id", $goods_id)->select(); // 商品 图册

        //当前用户收藏
        $user_id = cookie('user_id');

        //取10条评论消息
        $comment_list = M("comment")
                        ->alias("c")
                        ->join("f_users u","c.user_id = u.user_id","left")
                        ->where(array("c.goods_id" =>$goods_id,"c.is_show"=>1))
                        ->order("c.add_time desc")
                        ->field("c.*,u.head_pic")
                        ->limit(10)
                        ->select();
        foreach ($comment_list as $key => $value){
            $comment_list[$key]['img'] = unserialize($value['img']);
        }
        $this->assign('comment_list',$comment_list);
        $this->assign('goods_images_list',$goods_images_list);//商品缩略图
        $this->assign('goods',$goods);
        $this->assign('quantity',$quantity);//窖藏规格
        
        return $this->fetch();
    }
    
    /**
     * 商品定制页面
     */
    public function goodsDestine(){
    	if(empty(I('goods_id'))){
    		$err = '参数错误，请不要非法操作！';
    		$this->assign('err',$err);
    	}
    	$goods_id = I('goods_id');
    	$goods = M('goods')->alias('a')
                         ->join('destine_goods_attr b','b.goods_id = a.goods_id')
                         ->join('destine_theme c','b.theme_id = c.id')
                         ->join('destine_aroma e','b.aroma_id = e.id')
                         ->join('suppliers f','b.winery_id = f.suppliers_id')
                         ->join('origin g','g.id = f.origin_id')
                         ->field('a.*,b.*,g.title as originname,f.suppliers_address,
                         f.suppliers_name,f.brand_name,f.suppliers_logo,f.suppliers_id,
                         c.title as themename,e.title as aromaname')
                         ->where('b.goods_id',$goods_id)->find();
        if($goods){
        	$goods['tmp_info'] = json_decode($goods['tmp_info'],true);
        }                                                          
        $this->assign('goods',$goods);                 
    	return $this->fetch();
    }
    
    
    /**
     * 商品定制页面2
     */
    public function goodsDestine2(){
    	if(empty(I('goods_id'))){
    		$err = '参数错误，请不要非法操作！';
    		$this->assign('err',$err);
    	}
    	$goods_id = I('goods_id');
    	$goods = M('goods')->alias('a')
                         ->join('destine_goods_attr b','b.goods_id = a.goods_id')
                         ->join('destine_theme c','b.theme_id = c.id')
                         ->join('destine_aroma e','b.aroma_id = e.id')
                         ->join('suppliers f','b.winery_id = f.suppliers_id')
                         ->join('origin g','g.id = f.origin_id')
                         ->field('a.*,b.*,g.title as originname,f.suppliers_address,
                         f.suppliers_name,f.brand_name,f.suppliers_logo,f.suppliers_id,
                         c.title as themename,e.title as aromaname')
                         ->where('b.goods_id',$goods_id)->find();                                              
        $this->assign('goods',$goods);                 
    	return $this->fetch();
    }
    
    
    /**
     * 定制商品预览
     */
    public function goodsPreview(){
    	if(empty(I('goods_id'))){
    		$err = '参数错误，请不要非法操作！';
    		$this->assign('err',$err);
    	}
    	return $this->fetch();
    }
    
    
    /**
     * 定制商品DIY确定页面
     */
    public function doDetermine(){
    	if(empty(I('goods_id'))){
    		$err = '参数错误，请不要非法操作！';
    		$this->assign('err',$err);
    	}
    	return $this->fetch();
    }
    
    
    
    public function ajaxGetMore(){
    	$p = I('p/d',1);
    	$favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->page($p,10)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
    	$this->assign('favourite_goods',$favourite_goods);
    	return $this->fetch();
    }

    // 促销活动页面
    public function promoteList()
    {
        $goodsList = DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->limit(5)->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        return $this->fetch();
    }

    // 加载更多促销活动商品
    public function ajaxPromoteList(){
        $p = ((I('p/d')*5))+1;
        $goodsList = DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->page($p,5)->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        return $this->fetch();
    }
    
    /**
     * 个性案列
     * $num 显示的条数
     */
    public function caseList(){
        $start = empty(I('start'))?0:I('start');
    	$limit = empty(I('limit'))?10:I('limit');
        $goodsList = M('goods')->alias('a')
    	            ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name')
			    	->join('destine_goods_attr b','a.goods_id=b.goods_id')
			    	->join('destine_aroma c','b.aroma_id=c.id')
			    	->join('destine_theme e','b.theme_id=e.id')
			    	->join('suppliers f','f.suppliers_id=b.winery_id')
			    	->join('origin g','g.id=f.origin_id')->where("a.type",2)->where("is_on_sale",1)->order('a.shop_price')->limit($start,$limit)->select();
//         var_dump($goodsList);
//         exit();
        $this->assign('lists',$goodsList);
        return $this->fetch();
    }
    
    /*
     * 获取定制主题
     * $start   开始位置
     * $limit   获取的记录数
     */
    public function getDestineCase($start = 0,$limit=0){
        if(empty($limit)){
            $result = M('goods')->alias('a')
            ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name')
            ->join('destine_goods_attr b','a.goods_id=b.goods_id')
            ->join('destine_aroma c','b.aroma_id=c.id')
            ->join('destine_theme e','b.theme_id=e.id')
            ->join('suppliers f','f.suppliers_id=b.winery_id')
            ->join('origin g','g.id=f.origin_id')->where("type",2)->limit($start)->select();
        }else{
            $result = M('goods')->alias('a')
            ->field('a.*,b.*,c.title as aromaname,e.title as themetitle,f.suppliers_name')
            ->join('destine_goods_attr b','a.goods_id=b.goods_id')
            ->join('destine_aroma c','b.aroma_id=c.id')
            ->join('destine_theme e','b.theme_id=e.id')
            ->join('suppliers f','f.suppliers_id=b.winery_id')
            ->join('origin g','g.id=f.origin_id')->where("type",2)->limit($start,$limit)->select();
        }
        return $result;
    }
    
    
    /**
     * 设计师列表
     */
    public function designList(){
    	
    	$suppliers_id = I('suppliers_id/d');
        $lists = M('designer')->where('suppliers_id',$suppliers_id)->select();
        $this->assign('lists',$lists);
        return $this->fetch();
        
    }
    
    
    /**
     * 设计师详细页面
     */
    public function designInfo(){
    	
    	$id = I('id/d');
        $info = M('designer')->where('id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
        
    }
    

}